﻿using Survey.DataAccessLayer;
using Survey.DataAccessLayer.Data;
using Survey.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebUI.constant;
using WebUI.dto;
using WebUI.Units;

namespace WebUI.Business
{
    public class QuestionnaireBusiness
    {



        #region 问卷管理
        private QuestionnaireAccessor questionnaireAccessor = new QuestionnaireAccessor();
        //api/EditQuestionnaireById
        public Object EditQuestionnaireById(HttpSessionStateBase session, int id)
        {
            //身份验证
            var curentUserId = session["curentUserId"];
            //没有登陆
            if (curentUserId == null)
            {
                return Result<Questionnaire>.Build(CodeEnum.NOT_LOGIN
                   , CodeDictionary.GetCodeMsg(CodeEnum.NOT_LOGIN)
                   , null
                   , false);
            }


            try
            {
                //有这个问卷，且问卷属于操作者
                var q = questionnaireAccessor.Select(qq => qq.Id == id && qq.UserId == (int)curentUserId).Single();
                var result = Units.DataProcess.Obj2Json(q);
                return Result<Questionnaire>.Build(CodeEnum.EDITE_SUCCESS
                  , CodeDictionary.GetCodeMsg(CodeEnum.EDITE_SUCCESS)
                  , (Questionnaire)Units.DataProcess.Json2Obj(result, (new Questionnaire()).GetType())
                  , true);
            }
            catch (Exception)
            {

                return Result<String>.Build(CodeEnum.NOT_AUTHORITY_EDITE
                  , CodeDictionary.GetCodeMsg(CodeEnum.NOT_AUTHORITY_EDITE)
                  , null
                  , false);

            }

        }
        //api/CreateQuestionnaire
        public Object CreateQuestionnaire(HttpSessionStateBase session, Questionnaire questionnaire)
        {
            var curentUserId = session["curentUserId"];
            if (questionnaire == null)
            {
                return Result<String>.Build(CodeEnum.EMPTY_DATA_ERROR
                 , CodeDictionary.GetCodeMsg(CodeEnum.EMPTY_DATA_ERROR)
                 , null
                 , false);
            }
            if (curentUserId == null)
            {
                return Result<Questionnaire>.Build(CodeEnum.NOT_LOGIN
                  , CodeDictionary.GetCodeMsg(CodeEnum.NOT_LOGIN)
                  , null
                  , false);
            }
            try
            {

                questionnaire.UserId = (int)curentUserId;
                questionnaireAccessor.Add(questionnaire);
                questionnaireAccessor.Save();
                return Result<Questionnaire>.Build(CodeEnum.CREATE_QESTIONNAIRE_SUCCESS
                  , CodeDictionary.GetCodeMsg(CodeEnum.CREATE_QESTIONNAIRE_SUCCESS)
                  , null
                  , true);
            }
            catch (Exception e)
            {
                return Result<Questionnaire>.Build(CodeEnum.CREATE_QESTIONNAIRE_FAILED
                   , CodeDictionary.GetCodeMsg(CodeEnum.CREATE_QESTIONNAIRE_FAILED)
                   , null
                   , false);
            }
        }
        //api/DeleteQuestionnaireById
        public Object DeleteQuestionnaireById(HttpSessionStateBase session, int id)
        {
            var curentUserId = session["curentUserId"];
            if (curentUserId == null)
            {
                return Result<Questionnaire>.Build(CodeEnum.NOT_LOGIN
                   , CodeDictionary.GetCodeMsg(CodeEnum.NOT_LOGIN)
                   , null
                   , false);
            }
            if (id <= 0)
                return Result<Questionnaire>.Build(CodeEnum.EMPTY_DATA_ERROR
                   , CodeDictionary.GetCodeMsg(CodeEnum.EMPTY_DATA_ERROR)
                   , null
                   , false);
            try
            {
                //有这个问卷，且问卷属于自己
                var q = questionnaireAccessor.Select(qq => qq.Id == id && qq.UserId == (int)curentUserId).Single();

                questionnaireAccessor.Delete(q);

                questionnaireAccessor.Save();
                return Result<Questionnaire>.Build(CodeEnum.DELETE_SUCCESS
                   , CodeDictionary.GetCodeMsg(CodeEnum.DELETE_SUCCESS)
                   , null
                   , true);
            }
            catch (Exception e)
            {
                return Result<Questionnaire>.Build(CodeEnum.NOT_AUTHORITY_EDITE
                  , CodeDictionary.GetCodeMsg(CodeEnum.NOT_AUTHORITY_EDITE)
                  , null
                  , false);
            }
        }
        //api/PostEditedQuestionnaire
        public Object PostEditedQuestionnaire(HttpSessionStateBase session
            , Questionnaire q)
        {
            var curentUserId = session["curentUserId"];

            if (q.Id <= 0)
                return Result<Questionnaire>.Build(CodeEnum.EMPTY_DATA_ERROR
                  ,"问卷Id是"+ CodeDictionary.GetCodeMsg(CodeEnum.EMPTY_DATA_ERROR)
                  , null
                  , false);

            try
            {
                //存在问卷，且属于自己
                bool isOk = questionnaireAccessor.Select(qq => qq.Id == q.Id && qq.UserId == (int)curentUserId).Count() > 0;
                if (!isOk)
                {
                    return Result<Questionnaire>.Build(CodeEnum.NOT_AUTHORITY_EDITE
                 , CodeDictionary.GetCodeMsg(CodeEnum.NOT_AUTHORITY_EDITE)
                 , null
                 , false);
                }

                questionnaireAccessor.Update(q);
                questionnaireAccessor.Save();
                //return new { Result = true, Msg = "成功" };
                return Result<String>.Build(CodeEnum.EDITE_SUCCESS
               , CodeDictionary.GetCodeMsg(CodeEnum.EDITE_SUCCESS)
               , null
               , true);
            }
            catch (Exception e)
            {
                return Result<String>.Build(CodeEnum.EDITE_FAILED
               , CodeDictionary.GetCodeMsg(CodeEnum.EDITE_FAILED)
               , null
               , false);
            }

        }



        #endregion


    }
}